package com.kznnc.data_structure.structure.treeStructure;

/**
 * @Description   二叉搜索树
 * @Author houchenkai
 * @Created Date: 2021/4/12 16:30
 * @ClassName
 */
public class BinarySearchTree {
    /**
     * 节点的值
     */
    private Integer value;
    /**
     * 左子树
     */
    private BinarySearchTree leftTree;
    /**
     * 右子树
     */
    private BinarySearchTree rightTree;


    public BinarySearchTree(Integer value) {
        this.value = value;
    }

    /**
     * 待插入值 data
     * <p>
     * data 和 当前节点的 值进行比较
     * 比当前节点小的时候
     *
     * @param data
     */
    public void insertTreeNode(Integer data) {
        if (data == null) {
            throw new RuntimeException("空指针");
        }
        if (data <= value) {
            if (leftTree == null) {
                leftTree = new BinarySearchTree(data);
            } else {
                leftTree.insertTreeNode(data);
            }
        } else {
            if (rightTree == null) {
                rightTree = new BinarySearchTree(data);
            } else {
                rightTree.insertTreeNode(data);
            }
        }
    }


    public void deleteTreeNode(Integer data){


    }


    /**
     * 中序遍历  左 根  右
     *
     * 每个节点都会   找左子树 找到最左后 知道我是最后一个左节点 然后我别上自己是左  然后告诉 父节点 我是左  然后
     */
    public void printerData() {
        if (this.leftTree != null) {
            this.leftTree.printerData();
        }
            System.out.println(this.value);
        if (this.rightTree != null) {
            this.rightTree.printerData();
        }
    }




    public static void main(String[] args) {

        BinarySearchTree binarySearchTree = new BinarySearchTree(8);
        binarySearchTree.insertTreeNode(5);
        binarySearchTree.insertTreeNode(3);
        binarySearchTree.insertTreeNode(11);
        binarySearchTree.insertTreeNode(6);
        binarySearchTree.insertTreeNode(10);
        binarySearchTree.insertTreeNode(12);
        binarySearchTree.printerData();
        System.out.println(binarySearchTree);
    }


}
